﻿using System;
using System.Collections.Generic;
using System.Web; 
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SQLite;

public partial class SaleInfo_NewSale : UDCPage
{
	private int _did = -1;
    protected int DID
    {
        get { return _did; }
        set { _did = value; }
    }
	
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
			DID = String.IsNullOrEmpty(Request.QueryString["Did"])
                ? GetDid() : Int32.Parse(Request.QueryString["Did"].ToString());
            DevDataBind();
            if(!String.IsNullOrEmpty(Request.QueryString["sid"]))
            {
                sid.Text = Request.QueryString["sid"].ToString();                
                reset.Visible = false;
                submit.Visible = false;
                if (!IFReported(SaleBind(Int32.Parse(sid.Text))))
                {
                    save.Visible = true;
                    delete.Visible = true;
                }
                else
                {
                    msg.Text = "信息已统计完毕！";
                }
            }
            returnLink.HRef = "UnitDay.aspx?msg=NoFirst&did=" + DID.ToString();
            if (!String.IsNullOrEmpty(Request.QueryString["add"]))
            {
                returnLink.HRef = "AuditDetail.aspx?did=" + DID.ToString();
                isAdd.Text = "YES";
                did.Text = DID.ToString();
            }
        }
    }

    protected void submit_Click(object sender, EventArgs e)
    {
        if (AddSale())
        {
            if (isAdd.Text.Equals("YES"))
            {
                Response.Redirect("AuditDetail.aspx?did=" + did.Text);
            }
            else
            {
                Response.Redirect("UnitDay.aspx");
            }
        }
        else
        {
            msg.Text = "添加未成功！请重试";
        }
    }

    private bool AddSale()
    {
        string sqlstr = "insert into ud_saleinfo_t(sid,serviceid,iscontract,isnew,saledate,saledesc,operater,dvid) "
            + "values(null,$serviceid,$iscontract,$isnew,$saledate,$saledesc,$operater,$dvid)";
        SQLiteParameter[] spc = {
        new SQLiteParameter("$serviceid", DbType.String, 20),
        new SQLiteParameter("$iscontract", DbType.Boolean),
        new SQLiteParameter("$isnew", DbType.Boolean),
        new SQLiteParameter("$saledate", DbType.Date),
        new SQLiteParameter("$saledesc", DbType.String, 400),
        new SQLiteParameter("$operater", DbType.Int32),
        new SQLiteParameter("$dvid", DbType.Int32)
        };
        spc[0].Value = serviceId.Text.Trim();
        spc[1].Value = Boolean.Parse(IsContract.Items[IsContract.SelectedIndex].Value);
        spc[2].Value = Boolean.Parse(IsNew.Items[IsNew.SelectedIndex].Value);
        spc[3].Value = System.DateTime.Now;
        if (isAdd.Text.Equals("YES"))
        {
            spc[3].Value = System.DateTime.Now.AddDays(-1);
        }        
        spc[4].Value = SaleDesc.Value.Trim();
        spc[5].Value = Uid;
        spc[6].Value = Int32.Parse(DevList.SelectedValue);
        int result = SQLiteHelper.ExecuteNonQuery(sqlstr, spc);
        return result == 1;
    }

    private void DevDataBind()
    {
        string sqlstr = "select dvid,dvname from ud_developer_t where did = :did and ifvalid = 1";
        SQLiteParameter[] spc = { 
                                    new SQLiteParameter(":did",DbType.Int32)
                                };
        spc[0].Value = DID;
        DevList.DataSource = SQLiteHelper.ExecuteDataset(sqlstr, spc);
        DevList.DataTextField = "dvname";
        DevList.DataValueField = "dvid";
        DevList.DataBind();
        DevList.SelectedIndex = 0;
    }

    private int SaleBind(int sid)
    {
        string sqlstr = "select serviceid,iscontract,isnew,saledesc,dvid from ud_saleinfo_t where sid = $sid";
        SQLiteParameter[] spc = { 
                                    new SQLiteParameter("$sid",DbType.Int32)
                                };
        spc[0].Value = sid;
        DataRow dr = SQLiteHelper.ExecuteDataRow(sqlstr, spc);
        serviceId.Text = dr["serviceid"].ToString();
        serviceId.ReadOnly = true;
        IsContract.SelectedValue = dr["iscontract"].ToString();
        IsNew.SelectedValue = dr["isnew"].ToString();
        DevList.SelectedValue = dr["dvid"].ToString();
        SaleDesc.Value = dr["saledesc"].ToString();
        return Int32.Parse(dr["dvid"].ToString());
    }

    protected void save_Click(object sender, EventArgs e)
    {
        string sqlstr = @"update ud_saleinfo_t set iscontract = $iscontract,isnew = $isnew 
            ,saledesc = $saledesc,dvid = $dvid where sid = $sid";
        SQLiteParameter[] spc = { 
                                    new SQLiteParameter("$sid",DbType.Int32),
                                    new SQLiteParameter("$iscontract",DbType.Boolean),
                                    new SQLiteParameter("$isnew",DbType.Boolean),
                                    new SQLiteParameter("$dvid",DbType.Int32),
                                    new SQLiteParameter("$saledesc",DbType.String,200)
                                };
        spc[0].Value = Int32.Parse(sid.Text);
        spc[1].Value = Boolean.Parse(IsContract.Items[IsContract.SelectedIndex].Value);
        spc[2].Value = Boolean.Parse(IsNew.Items[IsNew.SelectedIndex].Value);
        spc[3].Value = Int32.Parse(DevList.SelectedValue);
        spc[4].Value = SaleDesc.Value.Trim();
        SQLiteHelper.ExecuteNonQuery(sqlstr, spc);
        Response.Redirect("UnitDay.aspx");
    }

    protected void delete_Click(object sender, EventArgs e)
    {
        string sqlstr = "delete from ud_saleinfo_t where sid = $sid";
        SQLiteParameter[] spc = { 
                                    new SQLiteParameter("$sid",DbType.Int32)
                                };
        spc[0].Value = Int32.Parse(sid.Text);
        SQLiteHelper.ExecuteNonQuery(sqlstr, spc);
        Response.Redirect("UnitDay.aspx");
    }

    private bool IFReported(int dvid)
    {
        string sqlstr = @"select count(1) from ud_reported_t t1
                             where date(repdate) = date('now','+8 hour') 
                               and exists(select 1 from ud_developer_t t2
                                           where t1.did = t2.did
                                             and t2.dvid = $dvid)";
        SQLiteParameter[] spc = { 
                                    new SQLiteParameter("$dvid",DbType.Int32)
                                };
        spc[0].Value = dvid;
        return Int32.Parse(SQLiteHelper.ExecuteScalar(sqlstr, spc).ToString()) > 0;
    }
}